package com.core.ITServiceLog.service;


import com.core.ITServiceLog.dao.ItserviceMenusDao;
import com.core.ITServiceLog.entity.ItserviceMenus;


import com.core.ITServiceLog.web.dto.ItserviceMenusExcelExportData;
import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.util.ExcelError;
import com.ibeetl.admin.core.util.PlatformException;
import org.apache.poi.ss.util.CellReference;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ItserviceMenus Service
 */

@Service
@Transactional
public class ItserviceMenusService extends CoreBaseService<ItserviceMenus>{

    @Autowired private ItserviceMenusDao itserviceMenusDao;

    public PageQuery<ItserviceMenus>queryByCondition(PageQuery query){
        PageQuery ret =  itserviceMenusDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelItserviceMenus(List<Long> ids){
        try {
            itserviceMenusDao.batchDelItserviceMenusByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除ItserviceMenus失败", e);
        }
    }

    public void batchInsert(List<ItserviceMenusExcelExportData> list) {
        final Map<Integer, ItserviceMenusExcelExportData> map = new HashMap<>();
        list.forEach((item) -> map.put(item.getId(), item));
        //逐个按照顺序导入
        for (ItserviceMenusExcelExportData item : list) {
            ItserviceMenus itsm = new ItserviceMenus();
            itsm.setId(item.getId());
            itsm.setSystem(item.getSystem());
            itsm.setModule(item.getModule());
            itsm.setFunction(item.getFunction());
            itsm.setEnable(item.getEnable());
            itsm.setCreatby(item.getCreatby());
            itsm.setCreatdt(item.getCreatdt());
            itsm.setLasteditby(item.getLasteditby());
            itsm.setLasteditdt(item.getLasteditdt());
            //导入前先查找是否有此值
            ItserviceMenus template = new ItserviceMenus();
            template.setId(item.getId());
            template.setSystem(item.getSystem());
            template.setModule(item.getModule());
            template.setFunction(item.getFunction());
            ItserviceMenus dbItsm = itserviceMenusDao.templateOne(template);
            if (dbItsm != null) {
//                int row = item.getExcelId() + dataStartRow;
//                throwImporError(row, 0, "模块已经存在");
                throw new PlatformException(item.getId()+"模块已经存在");
            }
            itserviceMenusDao.insert(itsm);
        }
    }
        private void throwImporError(int row,int col,String msg) {
            ExcelError error = new ExcelError();
            CellReference cr = new CellReference(row,col,false,false);
            error.setCell(cr.formatAsString());
            error.setMsg(msg);
            throw new PlatformException("导入错误在:"+error.getCell()+","+msg);
        }
}